/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sgt.supermarket.controller;

import java.text.ParseException;
import java.util.Date;
import sgt.supermarket.entity.*;
import sgt.supermarket.hibernatedao.HibernateDAOFactory;
import sgt.supermarket.util.Converter;

/**
 *
 * @author Administrator
 */
public class SampleInstaller {

    private static boolean sampleInstalled = false;

    public static synchronized void installSample() throws ParseException {
        if (!sampleInstalled) {
            internalInstallSample();
            sampleInstalled = true;
        } else {
            System.out.println("Sample is installed already");
        }

    }

    private static void internalInstallSample() throws ParseException {
        HibernateDAOFactory fac = new HibernateDAOFactory();

//create sample roles
        Role[] roles = {
            new Role("admin"),
            new Role("inventory-manager"),
            new Role("sales-manager"),
            new Role("sales-person"),
            new Role("customer-manager")
        };

        fac.getRoleDAO().save(roles);

        //create sample employees
        Employee phuongnd, taict, liemht, inventoryManager;
        Employee[] employees = new Employee[]{
            new Employee("admin", "Administrator", Sex.MALE,
            Converter.toDate("1986/9/26", "yyyy/M/d"), "123456", true, roles[0]),
            inventoryManager = new Employee("inventory-manager", "Inventory Manager", Sex.FEMALE,
            Converter.toDate("1989/10/22", "yyyy/M/d"), "123456", true, roles[1]),
            new Employee("sale-manager", "Sale Manager", Sex.FEMALE,
            Converter.toDate("1989/10/22", "yyyy/M/d"), "123456", true, roles[2]),
            new Employee("sale-person", "Sale Person", Sex.FEMALE,
            Converter.toDate("1989/10/22", "yyyy/M/d"), "123456", true, roles[2]),
            new Employee("customer-manager", "Customer Manager", Sex.FEMALE,
            Converter.toDate("1988/3/14", "yyyy/M/d"), "123456", true, roles[4]),
            phuongnd = new Employee("phuongnd08", "Nguyen Duc Phuong", Sex.MALE,
            Converter.toDate("1990/3/14", "yyyy/M/d"), "123456", true, roles[1],
            roles[2]),
            liemht = new Employee("tungttt06", "Tran Thi Tung", Sex.FEMALE,
            Converter.toDate("1990/3/14", "yyyy/M/d"), "123456", true, roles[3]),
            taict = new Employee("thaodth05", "Dinh Thi Thao", Sex.FEMALE,
            Converter.toDate("1990/3/14", "yyyy/M/d"), "123456", true, roles[3])
        };

        fac.getEmployeeDAO().save(employees);

        //create sample customers

        Customer[] customers = new Customer[]{
            new Customer("Tran Mai Thuy Dung", "0101", 0, "090",
            Converter.toDate("1986/9/26", "yyyy/M/d"), true, "123456"),
            new Customer("Nguyen Mai Phuong", "0102", 0, "091",
            Converter.toDate("1998/6/19", "yyyy/M/d"), false, "123456"),
            new Customer("Ngo Quynh", "0103", 0, "092",
            Converter.toDate("1986/9/26", "yyyy/M/d"), false, "123456"),
            new Customer("Nguyen The Anh", "0104", 0, "093",
            Converter.toDate("1986/9/26", "yyyy/M/d"), true, "123456"),
            new Customer("Ho Ngoc Ha", "0105", 0, "094",
            Converter.toDate("1986/9/26", "yyyy/M/d"), true, "123456"),
            new Customer("Ho Quynh Huong", "0106", 0, "095",
            Converter.toDate("1986/9/26", "yyyy/M/d"), true, "123456")
        };

        fac.getCustomerDAO().save(customers);


        //create sample categories
        Category beer, milk, washing, vegetables, fruit, meat;
        Category root = new Category("root",
                new Category("Foods",
                vegetables = new Category("Vegetables"),
                fruit = new Category("Fruit"),
                meat = new Category("Meat")),
                new Category("Drinks",
                milk = new Category("Milk"),
                beer = new Category("Beer")),
                washing = new Category("Washing"),
                new Category("Tooth brush"),
                new Category("Soap"));

        fac.getCategoryDAO().save(root);

        //create sample products;
        Product[] products = {
            new Product("Rau muong", "RAU-1", "Dalat", "Mot dang cap rau xanh", "Bundle",
            2000, vegetables,
            new ProductBlock(inventoryManager, 1000, new Date(), 50, 30),
            new ProductBlock(inventoryManager, 1200, new Date(), 40, 40)),
            new Product("Xa lach", "RAU-2", "Dalat", "Rau xanh cho moi nha", "Bo",
            2500, vegetables,
            new ProductBlock(phuongnd, 1000, new Date(), 50, 30),
            new ProductBlock(phuongnd, 1200, new Date(), 40, 40)),
            new Product("Carot", "RAU-3", "Dalat", "Giau carotene", "C?",
            2400, vegetables,
            new ProductBlock(phuongnd, 1000, new Date(), 50, 30),
            new ProductBlock(phuongnd, 1200, new Date(), 40, 40)),
            new Product("Khoai tay", "RAU-3", "Dalat", "Thuc pham dinh duong cho moi nguoi", "C?",
            5000, vegetables,
            new ProductBlock(phuongnd, 1000, new Date(), 50, 30),
            new ProductBlock(inventoryManager, 1200, new Date(), 40, 40)),
            new Product("Xoai gion", "FRU-1", "Camranh, Khanhhoa", "Xoai so zach", "Fruit",
            18000, fruit,
            new ProductBlock(inventoryManager, 3000, new Date(), 100, 10),
            new ProductBlock(inventoryManager, 3300, new Date(), 100, 100)),
            new Product("Chom chom", "FRU-2", "Mien Tay", "Ai chom khong?", "Fruit",
            20000, fruit,
            new ProductBlock(inventoryManager, 3000, new Date(), 100, 10),
            new ProductBlock(inventoryManager, 3300, new Date(), 100, 100)),
            new Product("Mang cut", "FRU-1", "Lai theu", "Trai cay cho VIP", "Fruit",
            50000, fruit,
            new ProductBlock(inventoryManager, 3000, new Date(), 100, 10),
            new ProductBlock(inventoryManager, 3300, new Date(), 100, 100)),
            new Product("Thit bo", "MEAT-1", "Long Thanh",
            "Rat nhieu Cholesterol", "Kg", 240000, meat,
            new ProductBlock(inventoryManager, 200000, new Date(), 100, 40),
            new ProductBlock(inventoryManager, 210000, new Date(), 50, 50)),
            new Product("Thit heo", "MEAT-2", "Bavifo", "Danh cho nguoi com",
            "Kg", 225000, meat,
            new ProductBlock(inventoryManager, 192000, new Date(), 50, 30),
            new ProductBlock(inventoryManager, 196000, new Date(), 40, 40)),
            new Product("Thit ga", "MEAT-3", "Bavifo", "Thom xuong ngot thit",
            "Kg", 225000, meat,
            new ProductBlock(inventoryManager, 192000, new Date(), 50, 30),
            new ProductBlock(inventoryManager, 196000, new Date(), 40, 40)),
            new Product("Sua chua Vinamilk", "YOG-1", "Vinamilk", "", "Boxes", 13000L, milk,
            new ProductBlock(inventoryManager, 9000, new Date(), 500, 145),
            new ProductBlock(inventoryManager, 9200, new Date(), 400, 400)),
            new Product("Sua tuoi Lothamilk", "FM-1", "Lothamilk", "", "Bottle", 6000L,
            milk,
            new ProductBlock(inventoryManager, 4000, new Date(), 300, 100),
            new ProductBlock(inventoryManager, 4200, new Date(), 200, 200))
        };
        products[0].addShrinkage(new Shrinkage(10, 1000, new Date()));

        fac.getProductDAO().save(products);
        //create sample invoices
        Invoice[] invoices = {
            new Invoice(
            customers[0],
            phuongnd, Converter.toDate("2009/12/28", "yyyy/M/d"),
            new InvoiceItem(products[0], 3, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[1], 1, products[1].getCurrentSellingPrice())),
            new Invoice(
            customers[0],
            phuongnd, Converter.toDate("2009/12/28", "yyyy/M/d"),
            new InvoiceItem(products[2], 3, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[3], 7, products[1].getCurrentSellingPrice())),
            new Invoice(
            customers[1],
            phuongnd, Converter.toDate("2009/12/28", "yyyy/M/d"),
            new InvoiceItem(products[3], 2, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[4], 5, products[1].getCurrentSellingPrice())),
            new Invoice(
            customers[0],
            phuongnd, Converter.toDate("2009/12/29", "yyyy/M/d"),
            new InvoiceItem(products[4], 1, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[5], 9, products[1].getCurrentSellingPrice())),
            new Invoice(
            customers[0],
            liemht, Converter.toDate("2009/12/28", "yyyy/M/d"),
            new InvoiceItem(products[0], 2, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[1], 3, products[1].getCurrentSellingPrice())),
            new Invoice(
            customers[0],
            liemht, Converter.toDate("2009/12/28", "yyyy/M/d"),
            new InvoiceItem(products[2], 3, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[3], 1, products[1].getCurrentSellingPrice())),
            new Invoice(
            customers[1],
            liemht, Converter.toDate("2009/12/28", "yyyy/M/d"),
            new InvoiceItem(products[3], 5, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[4], 1, products[1].getCurrentSellingPrice())),
            new Invoice(
            customers[5],
            taict, Converter.toDate("2009/12/29", "yyyy/M/d"),
            new InvoiceItem(products[4], 2, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[5], 4, products[1].getCurrentSellingPrice())),
            new Invoice(
            customers[4],
            taict, Converter.toDate("2009/12/28", "yyyy/M/d"),
            new InvoiceItem(products[0], 7, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[1], 3, products[1].getCurrentSellingPrice())),
            new Invoice(
            customers[3],
            taict, Converter.toDate("2009/12/28", "yyyy/M/d"),
            new InvoiceItem(products[2], 2, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[3], 1, products[1].getCurrentSellingPrice())),
            new Invoice(
            customers[1],
            taict, Converter.toDate("2009/12/28", "yyyy/M/d"),
            new InvoiceItem(products[3], 3, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[4], 2, products[1].getCurrentSellingPrice())),
            new Invoice(
            customers[2],
            liemht, Converter.toDate("2009/12/29", "yyyy/M/d"),
            new InvoiceItem(products[4], 4, products[0].getCurrentSellingPrice()),
            new InvoiceItem(products[5], 1, products[1].getCurrentSellingPrice())),};

        fac.getInvoiceDAO().save(invoices);
        // create sample Sale program

        SaleProgram[] salePrograms = {
            new SaleProgram(Converter.toDate("2009/12/13", "yyyy/M/d"),
            Converter.toDate("2010/01/01", "yyyy/M/d"), 100000, 0.01),
            new SaleProgram(Converter.toDate("2009/12/13", "yyyy/M/d"),
            Converter.toDate("2010/01/01", "yyyy/M/d"), 500000, 0.02),
            new SaleProgram(Converter.toDate("2009/12/13", "yyyy/M/d"),
            Converter.toDate("2010/01/01", "yyyy/M/d"), 1000000, 0.05)};
        fac.getSaleProgramDAO().save(salePrograms);
    }
}
